package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;

import entity.PageBean;
import entity.tArea;
import entity.tDevice;
import entity.tUser;
import entity.tUserAndtDevice;
import entity.tArea;
import service.tAreaService;
import service.tDeviceService;
import service.tUserAndtDeviceService;
import service.tUserService;

public class GetAlltDeviceAction {
	
	
	private int pageSize;
	private int page;    //第几页
    
    private PageBean pageBean;
    
	private String cpage;   
    private String total;   
    private String url;

	
	private tDeviceService tdeviceService;
	private tUserService tuserService;
	private tAreaService tareaService;
	private tUserAndtDeviceService uadservice;
	
	public String get() throws IOException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	  
	    HttpSession session = ServletActionContext.getRequest().getSession();
		//Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		System.out.println("----------经过GetAlltDeviceAction----------");
		
		tUser user = (tUser) session.getAttribute("user");
		session.setAttribute("user", user);
		String loginId = user.getUserId();
		Integer grade = user.getGrade();
		String tAreaId = user.gettAreaId();
		System.out.println(loginId+"   "+grade+"      "+tAreaId);
		List<tDevice> lists = new ArrayList<tDevice>();
		List<tUserAndtDevice> li = new ArrayList<tUserAndtDevice>();
		List<tArea> lis = new ArrayList<tArea>();
		
		if (grade == 2){
			
			this.pageBean = tdeviceService.searchAlltDevice(15,page);
			List<tDevice> list = pageBean.getList();
			JSONArray jsonArray = new JSONArray();
			for (tDevice tdevice : list){
				JSONObject t = new JSONObject();
				
				t.put("deviceNo", tdevice.getDeviceNo());
				t.put("deviceName", tdevice.getDeviceName());
				t.put("addTime", tdevice.getAddTime());
				t.put("deviceArea", tdevice.getDeviceArea());
				t.put("place", tdevice.getPlace());
				t.put("deviceDescribe", tdevice.getDeviceDescribe());
				t.put("mac", tdevice.getMac());
				t.put("deviceId", tdevice.getDeviceId());
				
				jsonArray.add(t);
			}
			System.out.println(jsonArray.toString());
			
			request.setAttribute("lists",list);
		}
		if (grade == 1){
			lis = tareaService.searchById(tAreaId);
			String name = lis.get(0).getName();
			this.pageBean = tdeviceService.searchBytArea(name,15,page);
			List<tDevice> list = pageBean.getList();
			JSONArray jsonArray = new JSONArray();
			for (tDevice tdevice : list){
				JSONObject t = new JSONObject();
				
				t.put("deviceNo", tdevice.getDeviceNo());
				t.put("deviceName", tdevice.getDeviceName());
				t.put("addTime", tdevice.getAddTime());
				t.put("deviceArea", tdevice.getDeviceArea());
				t.put("place", tdevice.getPlace());
				t.put("deviceDescribe", tdevice.getDeviceDescribe());
				t.put("mac", tdevice.getMac());
				t.put("deviceId", tdevice.getDeviceId());
				
				jsonArray.add(t);
			}
			System.out.println(jsonArray.toString());
			
			request.setAttribute("lists",list);
		}
		if (grade == 0){
			li = uadservice.searchByuserId(loginId);
			List<tDevice> lists1=new ArrayList();
			 for (tUserAndtDevice uad : li){
				 String deviceId = uad.getDeviceId();
				 System.out.println(deviceId);
				 //List<tDevice> list = new ArrayList<tDevice>();
				 this.pageBean = tdeviceService.searchById(deviceId,15,page);
				 
				 List<tDevice> list = pageBean.getList();
				
				 String mac = list.get(0).getMac();
				 String deviceid = list.get(0).getDeviceId();
				 String deviceNo = list.get(0).getDeviceNo();
				 String deviceName = list.get(0).getDeviceName();
				 Timestamp addTime = list.get(0).getAddTime();
				 String deviceArea = list.get(0).getDeviceArea();
				 String place = list.get(0).getPlace();
				 String deviceDescribe = list.get(0).getDeviceDescribe();
				 
				 JSONObject t = new JSONObject();
					
				 t.put("mac", mac);
				 t.put("deviceId", deviceid);
				 t.put("deviceNo", deviceNo);
				 t.put("deviceName", deviceName);
				 t.put("addTime", addTime);
				 t.put("deviceArea", deviceArea);
				 t.put("place", place);
				 t.put("deviceDescribe", deviceDescribe);
				 t.put("userId", loginId);
				 
				 System.out.println(t);
				 lists1.addAll(list);
				 
			 }
			 request.setAttribute("lists",lists1);
		}
		
		return "success";
	}
	
@SuppressWarnings("unchecked")
public String gettoindex() throws IOException{
	
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    @SuppressWarnings("unused")
	    HttpSession session = ServletActionContext.getRequest().getSession();
		//Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		System.out.println("----------经过GetAlltDeviceToIndexAction----------");
		
		tUser user = (tUser) session.getAttribute("user");
		session.setAttribute("user", user);
		System.out.println(user);
		String loginId = user.getUserId();
		Integer grade = user.getGrade();
		String tAreaId = user.gettAreaId();
		System.out.println(loginId+"   "+grade);
		List<tArea> lis = new ArrayList<tArea>();
		List<tDevice> lists = new ArrayList<tDevice>();
		List<tUserAndtDevice> li = new ArrayList<tUserAndtDevice>();
		
		if (grade == 2){
			
			this.pageBean = tdeviceService.searchAlltDevice(15,page);
			
			List<tDevice> list = pageBean.getList();
			
			JSONArray jsonArray = new JSONArray();
			for (tDevice tdevice : list){
				JSONObject t = new JSONObject();
				
				t.put("deviceNo", tdevice.getDeviceNo());
				t.put("deviceName", tdevice.getDeviceName());
				t.put("addTime", tdevice.getAddTime());
				t.put("deviceArea", tdevice.getDeviceArea());
				t.put("place", tdevice.getPlace());
				t.put("deviceDescribe", tdevice.getDeviceDescribe());
				t.put("mac", tdevice.getMac());
				t.put("deviceId", tdevice.getDeviceId());
				
				jsonArray.add(t);
			}
			System.out.println(jsonArray.toString());
			
			request.setAttribute("lists",list);
		}
		if (grade == 1){
			lis = tareaService.searchById(tAreaId);
			String name = lis.get(0).getName();
			this.pageBean = tdeviceService.searchBytArea(name,15,page);
			List<tDevice> list = pageBean.getList();
			JSONArray jsonArray = new JSONArray();
			for (tDevice tdevice : list){
				JSONObject t = new JSONObject();
				
				t.put("deviceNo", tdevice.getDeviceNo());
				t.put("deviceName", tdevice.getDeviceName());
				t.put("addTime", tdevice.getAddTime());
				t.put("deviceArea", tdevice.getDeviceArea());
				t.put("place", tdevice.getPlace());
				t.put("deviceDescribe", tdevice.getDeviceDescribe());
				t.put("mac", tdevice.getMac());
				t.put("deviceId", tdevice.getDeviceId());
				
				jsonArray.add(t);
			}
			System.out.println(jsonArray.toString());
			
			request.setAttribute("lists",list);
		}
		if (grade == 0){
			List<tDevice> lists1=new ArrayList();
			this.pageBean = uadservice.searchByuserId(loginId, 15, page);
			List<tUserAndtDevice> uads = pageBean.getList();
			 for (tUserAndtDevice uad : uads){
				 String deviceId = uad.getDeviceId();
				 System.out.println(deviceId);
				 List<tDevice> list = new ArrayList<tDevice>();
				 list = tdeviceService.searchById(deviceId);
				 
				 //List<tDevice> list = pageBean.getList();
				
				 String mac = list.get(0).getMac();
				 String deviceid = list.get(0).getDeviceId();
				 String deviceNo = list.get(0).getDeviceNo();
				 String deviceName = list.get(0).getDeviceName();
				 Timestamp addTime = list.get(0).getAddTime();
				 String deviceArea = list.get(0).getDeviceArea();
				 String place = list.get(0).getPlace();
				 String deviceDescribe = list.get(0).getDeviceDescribe();
				 
				 JSONObject t = new JSONObject();
					
				 t.put("mac", mac);
				 t.put("deviceId", deviceid);
				 t.put("deviceNo", deviceNo);
				 t.put("deviceName", deviceName);
				 t.put("addTime", addTime);
				 t.put("deviceArea", deviceArea);
				 t.put("place", place);
				 t.put("deviceDescribe", deviceDescribe);
				 t.put("userId", loginId);
				 
				 System.out.println(t);
				 lists1.addAll(list);
				 
			 }
			 request.setAttribute("lists",lists1);
		}
		
		List<tUser> tuser = new ArrayList<tUser>();
		JSONArray jsonArray = new JSONArray();
		tuser = tuserService.searchByGrade();
		
		for (tUser users : tuser){
			JSONObject t = new JSONObject();
			
			t.put("userName", users.getUserName());
			
			jsonArray.add(t);
		}
		
		List<tArea> tarea = new ArrayList<tArea>();
		
		tarea = tareaService.searchAll();
		 for (tArea tareas : tarea){
			JSONObject t = new JSONObject();
				
			t.put("name", tareas.getName());
				
			jsonArray.add(t);
		 }
		 
		request.setAttribute("tarea",tarea);
		
		request.setAttribute("tuser",tuser);
		
		//request.setAttribute("lists",lists);
		
		return "success";
	}
public String gettoadd() throws IOException{
	
	ActionContext context = ActionContext.getContext();  
    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
    @SuppressWarnings("unused")
	Map session = context.getSession(); 
	ServletResponse response = ServletActionContext.getResponse();
	response.setCharacterEncoding("utf-8");
	@SuppressWarnings("unused")
	PrintWriter out = response.getWriter();
	
	List<tArea> tarea = new ArrayList<tArea>();
	JSONArray jsonArray = new JSONArray();
	tarea = tareaService.searchAll();
	 for (tArea tareas : tarea){
		JSONObject t = new JSONObject();
			
		t.put("name", tareas.getName());
			
		jsonArray.add(t);
	 }
	 
	request.setAttribute("tarea",tarea);
	
	return "success";
}


	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public tDeviceService getTdeviceService() {
		return tdeviceService;
	}

	public void setTdeviceService(tDeviceService tdeviceService) {
		this.tdeviceService = tdeviceService;
	}

	public tUserService getTuserService() {
		return tuserService;
	}

	public void setTuserService(tUserService tuserService) {
		this.tuserService = tuserService;
	}

	public tAreaService getTareaService() {
		return tareaService;
	}

	public void setTareaService(tAreaService tareaService) {
		this.tareaService = tareaService;
	}

	public tUserAndtDeviceService getUadservice() {
		return uadservice;
	}

	public void setUadservice(tUserAndtDeviceService uadservice) {
		this.uadservice = uadservice;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	public String getTotal() {
		return total;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	

}
